package web;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import dto.AdminDto;
import dto.Admin_MenuDto;
import entity.Admin;
import enums.travelenums;
import service.Admin_service;

@Controller
public class Admin_controller {
	@Autowired
	private Admin_service admin_service;
	
	
	//主页面
	@RequestMapping("backindex")
	public String backIndex(HttpSession session,Model model){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			List<Admin_MenuDto> AdminMenuDtoList=admin_service.get_AdminMenu(admin);
			model.addAttribute("admin", admin);
			model.addAttribute("AdminMenuDtoList", AdminMenuDtoList);
			
			return "back/backindex";
		}
		return "back/backlogin";
	}
	//退出登陆
	@RequestMapping("adminout")
	public String adminout(HttpSession session){
		session.removeAttribute("admin");
		return "back/backlogin";
	}
	
	
	
	//管理员页面
	@RequestMapping(value="admin/manage",method=RequestMethod.POST)
	public String adminlist(Model model){		
		return "back/admin";
	}
	//管理员列表
	@RequestMapping(value="adminlist",method=RequestMethod.POST)
	@ResponseBody
	public List<AdminDto> adminList(){
		return admin_service.adminList();
	}
	//分配角色
	@RequestMapping(value="admin/role",method=RequestMethod.POST)
	@ResponseBody
	public String adminRole(Integer adminId,Integer groupId){
		if(adminId!=null&&groupId!=null){
			Admin admin=new Admin();
			admin.setId(adminId);
			admin.setGroupId(groupId);
			admin_service.update_admin(admin);
			return travelenums.SUCCESS.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	//新增或更新管理员
	@RequestMapping(value="admin/add_update",method=RequestMethod.POST)
	@ResponseBody
	public String AdminUpdateOrAdd(Admin admin){
		Admin Sql_admin=admin_service.getAdminById(admin.getId());
		if(Sql_admin!=null){
			Sql_admin.setName(admin.getName());
			Sql_admin.setPassword(admin.getPassword());
			boolean boo=admin_service.update_admin(Sql_admin);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}else{
			boolean boo=admin_service.add_admin(admin);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
	}
	//删除管理员
	@RequestMapping(value="admin/delete",method=RequestMethod.POST)
	@ResponseBody
	public String adminDelete(Admin admin){
		Admin Sql_admin=admin_service.get_admin(admin);
		if(Sql_admin!=null){
			boolean boo=admin_service.delete_admin(admin.getId());
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	
	/*管理员登录*/
	@RequestMapping(value="administratorlogin",method=RequestMethod.POST)
	@ResponseBody
	public String administratorLogin(Admin admin,HttpSession session){
		
		Admin _admin=admin_service.get_admin(admin);
		if(_admin!=null){
			session.setAttribute("admin", _admin);				
			return travelenums.SUCCESS.getInfo();
		}
		else {	
			return travelenums.ERRO.getInfo();
		}
	}
	/*登陆验证*/
	@RequestMapping(value="admin/nameAndpw",method=RequestMethod.POST)
	@ResponseBody
	public String QuerynameAndpw(@RequestParam(value="adminname")String name,
								@RequestParam(value="password",required=false)String password){
		Admin admin=new Admin();
		admin.setName(name);
		if(password!=null){
			admin.setPassword(password);
		}
		Admin _admin=admin_service.get_admin(admin);	
		if(_admin!=null)
		{ 	
			return travelenums.SUCCESS.getInfo(); }
		else {
			return travelenums.ERRO.getInfo();
		}
	}
}
